<?php

// Restrict Direct Access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

global $_MAMBOTS;
if (defined( '_JEXEC' )){
$GLOBALS['database'] = $database = JFactory::getDBO();
if( isset($mainframe) && is_object($mainframe)) {
				$url = $mainframe->isAdmin() ? $mainframe->getSiteURL() : JURI::base();
			} else {
				$url = JURI::base();
			}
$mosConfig_live_site = $GLOBALS['mosConfig_live_site']		= substr_replace($url, '', -1, 1);
			$mosConfig_absolute_path = $GLOBALS['mosConfig_absolute_path']	= JPATH_SITE;
			$mosConfig_cachepath = $GLOBALS['mosConfig_cachepath'] = JPATH_BASE.DS.'cache';
$mainframe->registerEvent( 'onSearch', 'botSearchJosCom15'  );
}else{
$_MAMBOTS->registerFunction( 'onSearch', 'botSearchJosCom'  );
}
// Register Search Function


// -------------------------------------------------------------------------------
// Mambot function
// -------------------------------------------------------------------------------

function botSearchJosCom( $search_term, $phrase='', $ordering='' ) {
  global $my, $database, $sppc_customer_group_id, $language;
  global $mosConfig_abolute_path, $mosConfig_offset;

  // Return Empty Array if NO Search Term
  $search_term = trim( $search_term ); 
  if ($search_term == '') return array();

  // Store Search Term in Session
  $_SESSION['searchword'] = $search_term;
  
$tlang = isset($_SESSION['language'])?$_SESSION['language']:1;

// groups
if(!isset($_SESSION['sppc_customer_group_id'])) { 
  $customer_group_id = '0';
  } else {
   $customer_group_id = $sppc_customer_group_id;
  }
  
//get the itemid
    $query = "SELECT params"
    . "\n FROM #__mambots"
    . "\n WHERE element = 'ecjc.searchbot'"
    . "\n AND folder = 'search'"
    ;
    $database->setQuery( $query );
    $id     = $database->loadResult();
    //$eitemid_arr = explode($id, "=");
    $eitemid_arr = explode("=", $id);
    //print_r($eitemid_arr);
    $eitemid = @$eitemid_arr[1];

  // Build Where Clause
  $wheres = array();
  switch ($phrase) {
    case 'exact':
      $wheres2 = array();
      $wheres2[] = "LOWER(p.products_model) LIKE '%$search_term%'";
      $wheres2[] = "LOWER(pd.products_name) LIKE '%$search_term%'";
      $wheres2[] = "LOWER(pd.products_description) LIKE '%$search_term%'";
      $wheres2[] = "LOWER(pc.categories_name) LIKE '%$search_term%'";
      $wheres2[] = "LOWER(pm.manufacturers_name) LIKE '%$search_term%'";
      $where = '(' . implode( ') OR (', $wheres2 ) . ')';
      break;
    case 'all':
    case 'any':
    default:
      $words = explode( ' ', $search_term );
      $wheres = array();
      foreach ($words as $word) {
        $wheres2 = array();
        $wheres2[] = "LOWER(p.products_model) LIKE '%$word%'";
        $wheres2[] = "LOWER(pd.products_name) LIKE '%$search_term%'";
        $wheres2[] = "LOWER(pd.products_description) LIKE '%$search_term%'";
        $wheres2[] = "LOWER(pc.categories_name) LIKE '%$search_term%'";
        $wheres2[] = "LOWER(pm.manufacturers_name) LIKE '%$search_term%'";
        $wheres[] = implode( ' OR ', $wheres2 );
      }
     $where = '(' . implode( ($phrase == 'all' ? ') AND (' : ') OR ('), $wheres ) . ')';
      break;
  }


  $order = null;
  switch ($ordering) {
    case 'newest':
    default:
      $order = 'p.products_date_added DESC';
      break;
    case 'oldest':
      $order = 'p.products_date_added ASC';
      break;
    case 'popular':
      $order = 'p.products_ordered DESC';
      break;
    case 'alpha':
      $order = 'pd.products_name ASC, p.products_model ASC, pc.categories_name ASC';
      break;
    case 'category':
      $order = 'pc.categories_name ASC, p.products_model ASC, pd.products_name ASC';
      break;
  }
  

  // Build Final Query
  $query = "SELECT CONCAT(pd.products_name,' (',p.products_model,')') AS title,"
  . "\n p.products_date_added AS created,"
  . "\n pd.products_description AS text,"
  . "\n pc.categories_name AS section,"
  . "\n CONCAT( 'index.php?option=com_oscommerce&amp;osMod=product_info&amp;Itemid=$eitemid&amp;products_id=', p.products_id ) AS href,"
  . "\n '2' AS browsernav"
  . "\n FROM #__osc_products AS p"
  . "\n LEFT JOIN #__osc_manufacturers AS pm ON pm.manufacturers_id = p.manufacturers_id"
  . "\n LEFT JOIN #__osc_products_description AS pd ON pd.products_id = p.products_id"
  . "\n LEFT JOIN #__osc_products_to_categories AS pca ON pca.products_id = p.products_id"
  . "\n LEFT JOIN #__osc_categories_description AS pc ON pc.categories_id = pca.categories_id"
  . "\n LEFT JOIN #__osc_categories as c on pc.categories_id = c.categories_id"
  . "\n WHERE ( $where )"
  . "\n AND p.products_status = '1'"
  . "\n and find_in_set('".$customer_group_id."', products_hide_from_groups) = 0 and find_in_set('" . $customer_group_id . "', categories_hide_from_groups) = 0"
  . "\n AND pd.language_id = '".$tlang."'"
  . "\n AND pc.language_id = '".$tlang."'"
  . "\n ORDER BY $order";

  // Process Query & Return Results
  $database->setQuery( $query );
  return $database->loadObjectList();
} // -- END botSearchJosCom --

function botSearchJosCom15( $search_term, $phrase='', $ordering='' ) {
  global $my, $database, $sppc_customer_group_id, $language;
  global $mosConfig_abolute_path, $mosConfig_offset;

  // Return Empty Array if NO Search Term
  $search_term = trim( $search_term ); 
  if ($search_term == '') return array();

  // Store Search Term in Session
  $_SESSION['searchword'] = $search_term;
  
$tlang = isset($_SESSION['language'])?$_SESSION['language']:1;

// groups
if(!isset($_SESSION['sppc_customer_group_id'])) { 
  $customer_group_id = '0';
  } else {
   $customer_group_id = $sppc_customer_group_id;
  }
  
//get the itemid
    $query = "SELECT params"
    . "\n FROM #__plugins"
    . "\n WHERE element = 'ecjc.searchbot'"
    . "\n AND folder = 'search'"
    ;
    $database->setQuery( $query );
    $id     = $database->loadResult();
    //$eitemid_arr = explode($id, "=");
    $eitemid_arr = explode("=", $id);
    //print_r($eitemid_arr);
    $eitemid = @$eitemid_arr[1];
    $eitemid = trim($eitemid);

  // Build Where Clause
  $wheres = array();
  switch ($phrase) {
    case 'exact':
      $wheres2 = array();
      $wheres2[] = "LOWER(p.products_model) LIKE '%$search_term%'";
      $wheres2[] = "LOWER(pd.products_name) LIKE '%$search_term%'";
      $wheres2[] = "LOWER(pd.products_description) LIKE '%$search_term%'";
      $wheres2[] = "LOWER(pc.categories_name) LIKE '%$search_term%'";
      $wheres2[] = "LOWER(pm.manufacturers_name) LIKE '%$search_term%'";
      $where = '(' . implode( ') OR (', $wheres2 ) . ')';
      break;
    case 'all':
    case 'any':
    default:
      $words = explode( ' ', $search_term );
      $wheres = array();
      foreach ($words as $word) {
        $wheres2 = array();
        $wheres2[] = "LOWER(p.products_model) LIKE '%$word%'";
        $wheres2[] = "LOWER(pd.products_name) LIKE '%$search_term%'";
        $wheres2[] = "LOWER(pd.products_description) LIKE '%$search_term%'";
        $wheres2[] = "LOWER(pc.categories_name) LIKE '%$search_term%'";
        $wheres2[] = "LOWER(pm.manufacturers_name) LIKE '%$search_term%'";
        $wheres[] = implode( ' OR ', $wheres2 );
      }
     $where = '(' . implode( ($phrase == 'all' ? ') AND (' : ') OR ('), $wheres ) . ')';
      break;
  }


  $order = null;
  switch ($ordering) {
    case 'newest':
    default:
      $order = 'p.products_date_added DESC';
      break;
    case 'oldest':
      $order = 'p.products_date_added ASC';
      break;
    case 'popular':
      $order = 'p.products_ordered DESC';
      break;
    case 'alpha':
      $order = 'pd.products_name ASC, p.products_model ASC, pc.categories_name ASC';
      break;
    case 'category':
      $order = 'pc.categories_name ASC, p.products_model ASC, pd.products_name ASC';
      break;
  }
  

  // Build Final Query
  $query = "SELECT CONCAT(pd.products_name,' (',p.products_model,')') AS title,"
  . "\n p.products_date_added AS created,"
  . "\n pd.products_description AS text,"
  . "\n pc.categories_name AS section,"
  . "\n CONCAT( 'index.php?option=com_oscommerce&amp;osMod=product_info&amp;Itemid=$eitemid&products_id=', p.products_id ) AS href,"
  . "\n '2' AS browsernav"
  . "\n FROM #__osc_products AS p"
  . "\n LEFT JOIN #__osc_manufacturers AS pm ON pm.manufacturers_id = p.manufacturers_id"
  . "\n LEFT JOIN #__osc_products_description AS pd ON pd.products_id = p.products_id"
  . "\n LEFT JOIN #__osc_products_to_categories AS pca ON pca.products_id = p.products_id"
  . "\n LEFT JOIN #__osc_categories_description AS pc ON pc.categories_id = pca.categories_id"
  . "\n LEFT JOIN #__osc_categories as c on pc.categories_id = c.categories_id"
  . "\n WHERE ( $where )"
  . "\n AND p.products_status = '1'"
  . "\n and find_in_set('".$customer_group_id."', products_hide_from_groups) = 0 and find_in_set('" . $customer_group_id . "', categories_hide_from_groups) = 0"
  //. "\n AND pd.language_id = '".$tlang."'"
  //. "\n AND pc.language_id = '".$tlang."'"
  . "\n ORDER BY $order";

  // Process Query & Return Results
  $database->setQuery( $query );
 
  $rows = $database->loadObjectList();

  return $rows;
  //echo $query;
  $results = array();
	if(count($rows))
	{
		foreach($rows as $row)
		{
			$results = array_merge($results, (array) $row);
		}
	}
//print_r($rows);
//die();
//	return $results;
} // -- END botSearchJosCom --
?>
